package cn.ante.ymcc.service.impl;

import cn.ante.ymcc.domain.CourseMarket;
import cn.ante.ymcc.domain.CourseUserLearn;
import cn.ante.ymcc.dto.PayAliNotifyDto;
import cn.ante.ymcc.mapper.CourseUserLearnMapper;
import cn.ante.ymcc.service.ICourseMarketService;
import cn.ante.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Nai
 * @since 2022-08-08
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {
    @Autowired
    private ICourseUserLearnService courseUserLearnService;

    @Autowired
    private ICourseMarketService courseMarketService;
    @Override
    public CourseUserLearn checkMediaPermission(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",loginId);
        wrapper.eq("course_id",courseId);
        return courseUserLearnService.selectOne(wrapper);
    }

    /**
     * 根据支付回调结果修改课程服务中的用户权限
     * 1.根据 扩展参数得到loginId + courseId
     * 2.根据loginId和courseId查询 购买记录
     * 3.查询不到 直接新增
     * 4.查询到了
     *  4.1 增加可光看时间  + 覆盖orderNo
     * @param dto
     */
    @Override
    public void addUserLearn(PayAliNotifyDto dto) {
        String extParams = dto.getExtParams();
        Map<String,Object> map = JSONObject.parseObject(extParams, Map.class);
        Long loginId = Long.valueOf(map.get("loginId").toString());
        String[] courseIds = map.get("courseId").toString().split(",");
        Date now = new Date();
        for (String id:courseIds) {
            Long courseId = Long.valueOf(id);
            CourseUserLearn userLearn = selectByLoginIdAndCourseId(loginId, courseId);
            CourseMarket courseMarket = courseMarketService.selectById(courseId);
            // 根基登陆这和课程id查找登录学习记录，记录不存在，就新增
            if (userLearn == null){
                CourseUserLearn courseUserLearn = new CourseUserLearn();
                courseUserLearn.setCourseId(courseId);
                courseUserLearn.setStartTime(now);
                courseUserLearn.setEndTime(DateUtils.addDays(now,courseMarket.getValidDays()));
                courseUserLearn.setCourseOrderNo(dto.getOrderNo());
                courseUserLearn.setLoginId(loginId);
                courseUserLearn.setState(0); // 0已购买  1过期
                courseUserLearn.setCreateTime(now);
                insert(courseUserLearn);
            }else {
                // 登录学习者存在就修改状态、观看时间、订单号覆盖之前的，一个人可以多次购买一个课程，要增加其观看天数

                // @TODO 这里时间设置有问题，不能发生改变
                if(now.after(userLearn.getEndTime())){
                    userLearn.setStartTime(new Date());
                    userLearn.setEndTime(DateUtils.addDays(now, courseMarket.getValidDays()));
                }else {
                    userLearn.setEndTime(DateUtils.addDays(userLearn.getEndTime(), courseMarket.getValidDays()));
                }
                userLearn.setCourseOrderNo(dto.getOrderNo());
                updateById(userLearn);
            }
        }
    }

    public CourseUserLearn  selectByLoginIdAndCourseId(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id", loginId);
        wrapper.eq("course_id", courseId);
        return courseUserLearnService.selectOne(wrapper);
    }
}
